package cn.unknownerror.ueteam.listener;

import cn.unknownerror.ueteam.event.JoinTeamEvent;
import cn.unknownerror.ueteam.event.LeaveTeamEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.UUID;

public class TeamListener implements Listener {
    @EventHandler
    public void onPlayerJoinTeam(JoinTeamEvent event){
        for (UUID member :event.getTeam().getMembers()){
            Player p =Bukkit.getPlayer(member);
            if(p==null) continue;
            if(!p.isOnline()) continue;
            if(p.getUniqueId().equals(event.getPlayer().getUniqueId())) continue;
            p.sendMessage("§a§l来自队伍的消息:§7§l新成员 §f"+event.getPlayer().getName()+"§7§l 加入队伍。");
        }
    }
    @EventHandler
    public void onPlayerLeaveTeam(LeaveTeamEvent event){
        String message;
            if (event.getTeam().getCaptain().equals(event.getPlayer().getUniqueId())) {
                message="§a§l来自队伍的消息：§c§l你所在的队伍被解散了。";
            }else{
                message="§a§l来自队伍的消息：§c§l成员 §f"+event.getPlayer().getName()+" §c§l退出队伍";
            }
        for(UUID member:event.getTeam().getMembers()){
            if(member.equals(event.getPlayer().getUniqueId())) continue;
            Player m =Bukkit.getPlayer(member);
            if(m!=null) m.sendMessage(message);
        }
    }
}
